<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>选项卡</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    ul {
      list-style: none;
    }

    .box {
      width: 300px;
      height: 250px;
      border: 1px solid #000;
      margin: 10px auto;
    }

    #nav {
      height: 30px;
      overflow: hidden;
      background-color: #333;
      color: #fff;
      display: flex;
      text-align: center;
    }

    #nav li {
      float: left;
      line-height: 30px;
      flex: 1;
    }

    #content div {
      display: none;
      /* 垂直居中 */
      text-align: center;
      line-height: 200px;
    }

    .current1 {
      background-color: #ddd;
      color: black;
    }

    .current {
      display: block !important;
    }
  </style>
  <script>
    // 主要思路：记住每次点击的下标，然后去找下标对应的div
    window.onload = function () {
      var navs = document.getElementById('nav').getElementsByTagName('li')
      var contents = document.getElementById('content').getElementsByTagName('div')
      for (var i = 0; i < navs.length; i++) {
        navs[i].index = i;
        navs[i].onclick = function () {
          // 排他
          for (var i = 0; i < navs.length; i++) {
            navs[i].className = '';
            contents[i].className = '';
          }
          this.className = 'current1';
          contents[this.index].className = 'current'
        }
      }
    }
  </script>
</head>

<body>
  <div class="box">
    <ul id="nav">
      <li class="current1">新闻</li>
      <li>音乐</li>
      <li>视频</li>
    </ul>
    <div id="content">
      <div class="current">
        <h1>新闻内容</h1>
      </div>
      <div>
        <h1>音乐内容</h1>
      </div>
      <div>
        <h1>视频内容</h1>
      </div>
    </div>
  </div>
</body>

</html>